<?php

/**
* alltosun.com 核销记录列表接口 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 高昂 (gaoa@alltosun.com) $
* $Date: 2015-7-3 上午9:42:33 $
* $Id$
*/

class store_verify_vocher_record_widget
{
    // 每页显示条数
    private $per_page = 20;

    public function get_store_verify_vocher_list($user_id, $group_platform_id = 0, $type = '', $page_no = 1)
    {
        // 核销码,平台名称,兑换时间
        $filter = array();
        $filter['status'] = 1;
        $filter['user_id'] = $user_id;
        if ($group_platform_id != 0) {
            $filter['group_platform_id'] = $group_platform_id;
        }

        if ($type != '') {
            $filter['type'] = $type;
        }
        
        // 分页
        $total_count = _model('store_verify_vocher_record')->getTotal($filter);
        if (!$total_count) {
            return false;
        }
        $pager = new Pager($this->per_page);
        // 分配页数
        if ($pager->generate($total_count)) {
            // 计算总页数
            $pages = $pager->getPages();
        } else {
            $pages = 1;
        }
        
        if ($page_no > $pages || $page_no < 1) {
        	return array();
        }
        
        $record_list = _model('store_verify_vocher_record')->getList($filter, 'ORDER BY `id` DESC '.$pager->getLimit($page_no));
        
        $record_id_list = array();
        //获取当前页面核销记录id
        foreach ($record_list as $k=>$v) {
            $record_id_list[$k] = $v['id'];
        }
        
        
        // 存储日期
        $day_list = array();
        if ($record_list) {
            foreach ($record_list as $k => $v) {
                // 获取日期截止到天
                $cur_day = explode(' ', $v['add_time']);
                if (!in_array($cur_day[0], $day_list)) {
                    array_push($day_list, $cur_day[0]);
                }
            }

            $store_verify_vocher_list = array();
            $store_verify_vocher_list['page'] = array();
            $store_verify_vocher_list['page']['pages'] = $pages;
            $store_verify_vocher_list['page']['page_no'] = $page_no;
            //获取核销类型
            $store_verify_vocher_list['type_list'] = array();
            $type_list = _model('store_verify_vocher_record')->getList(array('status'=>1), ' GROUP BY `type`');
            if ($type_list) {
                foreach ($type_list as $k => $v) {
                    $store_verify_vocher_list['type_list'][$k]['type'] = $v['type']; 
                }
            } else {
                array_push($store_verify_vocher_list['type'], '暂无分类');
            }
            //获取核销平台的列表
            $store_verify_vocher_list['group_platform_list'] = array();
            $platform_list = _model('group_platform')->getList(array('status'=>1), 'ORDER BY `sort` DESC');
            if ($platform_list) {
                foreach ($platform_list as $k => $v) {
                    $store_verify_vocher_list['group_platform_list'][$k]['group_platform_id']   = $v['id'];
                    $store_verify_vocher_list['group_platform_list'][$k]['group_platform_name'] = $v['group_platform_name'];
                }
            } else {
                $store_verify_vocher_list['group_platform_list'] = '暂无平台';
            }


            foreach ($day_list as $k => $v) {

                $date_filter = array();
                $date_filter['status'] = 1;
                $date_filter['`add_time` <= '] = $v.' 23:59:59';
                $date_filter['`add_time` >= '] = $v.' 00:00:00';
                if ($group_platform_id) {
                    $date_filter['group_platform_id'] = $group_platform_id;
                }
                if ($type) {
                 $date_filter['type'] = $type;
                }
               
                $rec_list = array();
                $rec_list = _model('store_verify_vocher_record')->getList($date_filter, 'ORDER BY `id` DESC '.$pager->getLimit($page_no));
                $rec_num = _model('store_verify_vocher_record')->getTotal($date_filter, 'ORDER BY `id` DESC');
                if ($rec_list) {

                  // 日期
                  $store_verify_vocher_list['data'][$k]['date'] = $v;
                  foreach ($rec_list as $key => $val) {
                      $platform_info = _uri('group_platform', $val['group_platform_id']);
                      if ($platform_info) {
                          $rec_list[$key]['group_platform_name'] = @$platform_info['group_platform_name'];
                      }
                      
                  }
                  //生成返回的数据
                  $store_verify_vocher_list['data'][$k]['record_num'] = $rec_num;
                  foreach ($rec_list as $key => $val) {
                      $store_verify_vocher_list['data'][$k]['record_list'][$key]['id'] = $val['id'];
                      $store_verify_vocher_list['data'][$k]['record_list'][$key]['discount_code'] = $val['discount_code'];
                      $store_verify_vocher_list['data'][$k]['record_list'][$key]['group_platform_id'] = $val['group_platform_id'];
                      $store_verify_vocher_list['data'][$k]['record_list'][$key]['group_platform_name'] = @$val['group_platform_name'];
                      $store_verify_vocher_list['data'][$k]['record_list'][$key]['add_time'] = $val['add_time'];
                      $store_verify_vocher_list['data'][$k]['record_list'][$key]['type'] = $val['type'];
                  }
                }
               
            }

            return $store_verify_vocher_list;
           
        } else {
            return false;
        }
    }
}